package com.wawayaya.bookreader.ViewPages;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.wawayaya.bookreader.Imutualaction;
import com.wawayaya.util.Constants;
import com.wawayaya.util.NetUtils;
import com.wawayaya.util.TLog;
import java.util.concurrent.atomic.AtomicBoolean;
import wawayaya.kids_iread.R;

public class NewQuizView extends RelativeLayout {

	private TextView m_tvquizfailed = null;
	private WebView m_wvquizhtml = null;
	private ProgressBar m_pbloadonlinequiz = null;
	private String[] m_squizurls = null;
	private quizWebViewClient m_quizwvc = null;
	private quizWebChromeClient m_quizwcc = null;
	private AtomicBoolean m_loadquizsucc = new AtomicBoolean(false);
	private Handler m_hquitquiz = new Handler();
    private Imutualaction m_mutualactioncallback = null;

    private Context mContext;
	
	private final static int MSG_QUIZFAILED_ANIMATION_START = 0;
	private final static int MSG_QUIZFAILED_ANIMATION_CONTINUE = 1;
	
	public NewQuizView(Context context) {
		super(context);
        mContext =  context;
	}

	public NewQuizView(Context context, AttributeSet attrs) {

        super(context, attrs);
        mContext =  context;
	}
	
	public NewQuizView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
        mContext =  context;
	}
	
	public void recycle(){
/*        if(null != m_wvquizhtml) {
            m_wvquizhtml.setVisibility(View.GONE);
            m_wvquizhtml.loadUrl("");
            //m_wvquizhtml.clearCache(true);
            m_wvquizhtml.clearHistory();
            m_wvquizhtml.clearView();
        }*/
	}

    @SuppressLint("JavascriptInterface")
    public void findviews() {

        m_tvquizfailed = (TextView)findViewById(R.id.tv_quizfailed);
        m_wvquizhtml = (WebView)findViewById(R.id.wv_quizhtml);
        m_wvquizhtml.setBackgroundColor(Color.TRANSPARENT);

        WebSettings webSettings = m_wvquizhtml.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);

        m_wvquizhtml.addJavascriptInterface(
                new Object() {
                    public void quitquiz(){
                        TLog.log("NewQuizView : addJavascriptInterface : ");
                        m_hquitquiz.post(new Runnable(){
                            @Override
                            public void run() {
                                quitquizonlineview();
                            }
                        });
                    }
                },
                Constants.WAWAYAYA);
        m_quizwvc = new quizWebViewClient();
        m_wvquizhtml.setWebViewClient(m_quizwvc);
        m_quizwcc = new quizWebChromeClient();
        m_wvquizhtml.setWebChromeClient(m_quizwcc);


        m_pbloadonlinequiz = (ProgressBar)this.findViewById(R.id.pb_loadquizurl);



    }

    public void setquizurls(String[] squizurls) {
        m_squizurls = squizurls;

    }
	

    public void quitquizonlineview() {

        if(!m_loadquizsucc.get()){
            m_tvquizfailed.setVisibility(View.VISIBLE);
        }
        else{
            m_tvquizfailed.setVisibility(View.INVISIBLE);
        }
        //}
        if(null != m_wvquizhtml) {
            m_wvquizhtml.setVisibility(View.GONE);
            m_wvquizhtml.loadUrl("");
            //m_wvquizhtml.clearCache(true);
            m_wvquizhtml.clearHistory();
            m_wvquizhtml.clearView();
        }
        if(null != m_pbloadonlinequiz){
            m_pbloadonlinequiz.setVisibility(View.GONE);

        }
    }

	
	public void refreshinformation() {
        TLog.log("NewQuizView : refreshinformation : " + m_squizurls[0]);
        if (m_squizurls != null && m_squizurls.length > 0) {
            if (!m_loadquizsucc.get()) {
                TLog.log("NewQuizView : enter refreshinformation : " + m_squizurls[0]);
                m_loadquizsucc.set(true);

                WebSettings webSettings = m_wvquizhtml.getSettings();
                if (NetUtils.checkNetworkConnected(mContext)) {
                    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
                } else {
                    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                }

                m_tvquizfailed.setVisibility(View.INVISIBLE);
                m_wvquizhtml.setVisibility(View.VISIBLE);
                if(null != m_pbloadonlinequiz) {
                    m_pbloadonlinequiz.setVisibility(View.VISIBLE);
                }
                try{
                    TLog.log("CMD quiz " + m_squizurls[0]);
                    m_wvquizhtml.loadUrl(m_squizurls[0]);
                }catch(Exception e){
                }
            }
        }

	}

	
	private class quizWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
            TLog.log("NewQuizView : quizWebViewClient : shouldOverrideUrlLoading : " + url);
            if (url.equals("localcall:quitquiz")) {
                m_mutualactioncallback.getToCover();
                view.loadUrl(m_squizurls[0]);
            } else {
                view.loadUrl(url);
            }
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
            TLog.log("NewQuizView : quizWebViewClient : onPageFinished : " + url);
			if(null != m_pbloadonlinequiz) {
				m_pbloadonlinequiz.setVisibility(View.GONE);
			}
		}
		 
		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            TLog.log("NewQuizView : quizWebViewClient : onReceivedError : " + description + "errorcode = " + errorCode + " failingUrl = " + failingUrl);
            if (failingUrl.equals("localcall:quitquiz")) {
               // m_mutualactioncallback.getToCover();
            } else {
                m_loadquizsucc.set(false);
                quitquizonlineview();
            }
		}
		
	}
	
	private class quizWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if(newProgress < 100) {
			} 
			else {
				//TLog.log("quizView : quizWebChromeClient : onProgressChanged : " + 100);
			}
		}
	}

    public void setmutualactioncallback(Imutualaction callback) {
        m_mutualactioncallback = callback;
    }
	 
}
